package com.pj.hellomvvm.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.pj.common.base.toolbar.BaseToolbarRecyclerActivity
import com.pj.common.decorator.DividerItemDecoration
import com.pj.hellomvvm.R
import com.pj.hellomvvm.databinding.LayoutTestItemBinding
import com.pj.hellomvvm.viewmodel.TestRecyclerViewModel

class TestRecyclerviewActivity: BaseToolbarRecyclerActivity<TestRecyclerViewModel>() {

    companion object{
        fun actionStart(cxt:Context){
            cxt.startActivity(Intent(cxt, TestRecyclerviewActivity::class.java))
        }
    }
    private val mAdapter = MyAdapter()
    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mCBinding.recyclerView.adapter = mAdapter
        mCBinding.recyclerView.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST))
    }

    override fun loadData(pullToRefresh: Boolean) {
        super.loadData(pullToRefresh)
        mViewModel.loadData(pullToRefresh)
    }

    override fun uiObserve() {
        super.uiObserve()
        mViewModel.list.observe(this){
            if (mPullToRefresh) mAdapter.setNewInstance(it) else mAdapter.addData(it)
        }
    }

    class MyAdapter:BaseQuickAdapter<String,BaseDataBindingHolder<LayoutTestItemBinding>>(R.layout.layout_test_item){
        override fun convert(holder: BaseDataBindingHolder<LayoutTestItemBinding>, item: String) {
            holder.dataBinding?.apply {
                text = item
            }
        }

    }
}